package com.troyhigh.njrotc.admintrackerold;

import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import java.io.File;

/**
 * Thread-safe GUI file chooser
 * 
 * @author Albert Ou 
 * @version 1.0 2008-September-07
 */
public class ATXFileChooser
{
    JFileChooser chooser;
    
    /**
     * Constructs and displays a FileChooser
     * @param   mode    The type of files to be displayed
     * @param   approveButtonText   The text of the ApproveButton
     */
    public ATXFileChooser(int mode, String approveButtonText)
    {
        chooser = new JFileChooser();
        chooser.setFileSelectionMode(mode);
        int returnValue;
        do
        {
            returnValue = chooser.showDialog(null, approveButtonText);
        }
        while(returnValue != JFileChooser.APPROVE_OPTION);
    }
    
    public File getSelectedFile()
    {
        return chooser.getSelectedFile();
    }
    
    /**
     * Sets the custom save directory of ATXLibraryDriver to a user-specified path
     */
    public static void selectSaveDirectory()
    {
        // Schedule a job for the event-dispatching thread
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                ATXFileChooser chooser = new ATXFileChooser(JFileChooser.DIRECTORIES_ONLY, "Select Save Directory");
                ATXLibraryDriver.setCustomDirectory(chooser.getSelectedFile());
            }
        });
    }

}
